import CryptoJS from "crypto-js";
import w_md5 from "js_sdk/zww-md5/w_md5.js";

const COMMON_BASE_URL = "https://api-be.ele.me";

// saas商家编码（erp接入必填），固定的
const MERCHANT_CODE = "MMHG";

const ERP_STORE_CODE = "MMHGGDZS00149";

// 固定参数
const encrypt = "aes";
const version = 3;
// 饿了么零售平台账号秘钥
const secret = "eL4O0mFOxV";
const source = "40351124";

function genTicket() {
  return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11)
    .replace(/[018]/g, (c) =>
      (
        c ^
        (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))
      ).toString(16)
    )
    .toUpperCase();
}

// 签名计算方法
function signMethod({ cmd, timestamp, body, ticket }) {
  const signData = {};

  // 原始参数
  signData["cmd"] = cmd;
  signData["encrypt"] = encrypt;
  signData["secret"] = secret;
  signData["source"] = source;
  signData["ticket"] = ticket;
  signData["timestamp"] = timestamp; // 当前秒级时间戳
  signData["version"] = version;
  signData["body"] = body;

  // 对参数对象的键进行排序
  const sortedKeys = Object.keys(signData).sort();

  // 拼接参数字符串
  let tmp = [];
  sortedKeys.forEach((key) => {
    tmp.push(`${key}=${signData[key]}`);
  });

  // 用&拼接成字符串
  const strSign = tmp.join("&");

  // 计算MD5签名并转换为大写
  const sign = CryptoJS.MD5(strSign).toString().toUpperCase();

   
  return sign;
}

// 构建饿了么零售平台查询商品链接
export function buildElmGetGoodsUrl({
  shopId, // 店铺ID
  cmd,
  upc, // 商品编码
  ticket, // sku ticket
  timestamp, // 秒级时间戳
}) {
  const sign = signMethod({
    cmd,
    body: `{"shop_id":"${shopId}","upc":"${upc}"}`,
    ticket,
    timestamp,
  });
  return `${COMMON_BASE_URL}/?body=%7B%22shop_id%22%3A%22${shopId}%22%2C%22upc%22%3A%22${upc}%22%7D&cmd=${cmd}&encrypt=${encrypt}&sign=${sign}&source=${source}&ticket=${ticket}&timestamp=${timestamp}&version=${version}`;
}

 

// 构建saas平台 查询商品接口
export function buildSaasGetGoodsUrl({
  cmd, //saas.goods.store.query.batch
  skuId, // 商品编码
  timestamp, // 秒级时间戳
}) {
  const sigh = signMethod(
    {
      body:`{"merchant_code":"MMHG","page_no":1,"erp_store_code":"${ERP_STORE_CODE}","sku_code_list":["${skuId}"],"page_size":10}`,
      cmd:"saas.goods.store.query.batch",
      ticket:"0AD6EF5E-A355-42A3-A27C-A6FB1A8B0F10",
      timestamp:timestamp,
    },
    secret
  );

  return `${COMMON_BASE_URL}/?body=%7B%22merchant_code%22%3A%22${MERCHANT_CODE}%22%2C%22page_no%22%3A1%2C%22erp_store_code%22%3A%22${ERP_STORE_CODE}%22%2C%22sku_code_list%22%3A%5B%22${skuId}%22%5D%2C%22page_size%22%3A10%7D&cmd=${cmd}&encrypt=${encrypt}&sign=${sigh}&source=${source}&ticket=0AD6EF5E-A355-42A3-A27C-A6FB1A8B0F10&timestamp=${timestamp}&version=${version}`;
}


// 构建saas平台 修改商品库存接口
export function buildSaasUpdateStockUrl({ 
  skuId, // 商品编码
  stock, // 库存
  timestamp, // 秒级时间戳
}) {
    const sign = signMethod(
        {
          body:`{"merchant_code":"MMHG","sku_list":[{"sku_code":"5005910635415","quantity":3}],"erp_store_code":"MMHGGDZS00149"}`,
          cmd:"saas.sku.stock.update.batch",
          ticket:"5449E47C-0417-4C39-A54D-6C627B1A8655",
          timestamp:1734028142,
        },
        secret
      );

      console.log('signsignsignsignsign',sign);
      
    return `${COMMON_BASE_URL}/?body=%7B%22merchant_code%22%3A%22${MERCHANT_CODE}%22%2C%22sku_list%22%3A%5B%7B%22quantity%22%3A${stock}%2C%22sku_code%22%3A%22${skuId}%22%7D%5D%2C%22erp_store_code%22%3A%22${ERP_STORE_CODE}%22%7D&cmd=saas.sku.stock.update.batch&encrypt=${encrypt}&sign=${sign}&source=${source}&ticket=5449E47C-0417-4C39-A54D-6C627B1A8655&timestamp=1734028142&version=${version}`
}